package com.xinqizhi.service.impl;

import com.common.utils.PageUtils;
import com.xinqizhi.mapper.SysMenuMapper;
import com.xinqizhi.entity.SysMenuEntity;
import com.xinqizhi.pojo.SysMenuPojo;
import com.xinqizhi.service.SysMenuService;
import com.xinqizhi.vo.MenuNotInRoleVo;
import com.xinqizhi.vo.SysMenuVo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class SysMenuServiceImpl implements SysMenuService {
    private final static Logger log = Logger.getLogger(SysMenuServiceImpl.class);

    @Autowired
    private SysMenuMapper sysMenuMapper;


    @Override
    public List<SysMenuVo> selecRightMenuByPid() {
        List<HashMap<String, Object>> menuList=null;
        try {
           menuList  = sysMenuMapper.selectRightMenuList();
        }catch (Exception e){
            e.printStackTrace();
            log.info("查询菜单出现异常"+e.getMessage());
            return null;
        }
        List<SysMenuVo> menuVoList = this.menuMap2List(menuList);
        return menuVoList;
    }

    /**
     * 查询所有菜单用于管理，并分页
     * @return
     */
    @Override
    public List<SysMenuEntity> selectMenuList() {
        List<SysMenuEntity> allmenu = sysMenuMapper.selectMneuList();
        return allmenu;
    }

    /**
     * 通过menu_id获取menu信息
     * @param menuid
     * @return
     */
    @Override
    public Map<String, Object> selectMneuById(Integer menuid) {
        List<SysMenuEntity> entities = sysMenuMapper.selectMneuById(menuid);
        HashMap<String, Object> result = new HashMap<>();
        List<SysMenuPojo> menulist = new ArrayList();

        for (SysMenuEntity e:entities){
            SysMenuPojo pojo = new SysMenuPojo();
            pojo.setMenu_id(e.getMenuId());
            pojo.setMenu_name(e.getMenuName());
            pojo.setMenu_icon(e.getMenuLogo());
            pojo.setMenu_disc(e.getMenuDiscript());
            pojo.setMenu_status(e.getMenuStatus());
            menulist.add(pojo);
        }

        result.put("data",menulist.get(0));
        result.put("count",entities.size());
        return result;
    }

    /**
     * 跟新menu信息
     * @param pojo
     * @return
     */
    @Override
    public Map updateMenuById(SysMenuPojo pojo) {
        Map<String, Object> result = new HashMap<>();
        SysMenuEntity menuEntity = new SysMenuEntity();
        menuEntity.setMenuId(pojo.getMenu_id());
        menuEntity.setMenuName(pojo.getMenu_name());
        menuEntity.setMenuLogo(pojo.getMenu_icon());
        menuEntity.setMenuDiscript(pojo.getMenu_disc());
        menuEntity.setMenuStatus(pojo.getMenu_status());

       Boolean re= sysMenuMapper.updateMenuById(menuEntity);
       if (re==true){
           result.put("success",true);
       }else {
           result.put("success",false);
       }
        return result;
    }

    /*
            菜单map转list，并生成树结构
     */
    private List<SysMenuVo> menuMap2List(List<HashMap<String, Object>> menuMapList) {
        List<SysMenuVo> menuVos = new ArrayList<SysMenuVo>();
        for (HashMap<String, Object> menu : menuMapList) {
            SysMenuVo menuVo = new SysMenuVo();
            menuVo.setMenuId((Integer) menu.get("menu_id"));
            menuVo.setMenuName((String) menu.get("menu_name"));
            menuVo.setParentMenuId((Integer) menu.get("parent_menu_id"));
            menuVo.setMenu_icon((String) menu.get("menu_logo"));
            menuVo.setMenuUrl((String) menu.get("menu_url"));
            menuVos.add(menuVo);
        }
        List<SysMenuVo> menuTree = createMenuTree(menuVos);
        menuTree.sort(new Comparator<SysMenuVo>() {
            @Override
            public int compare(SysMenuVo o1, SysMenuVo o2) {
                if (o1.getMenuId() >= o2.getMenuId()) {
                    return 1;
                } else{
                    return -1;
                }
            }
        });
        return menuTree;
    }

    private List createMenuTree(List<SysMenuVo> menuVos){
        ArrayList<SysMenuVo> sysMenuVos = new ArrayList<>();
        for (SysMenuVo v:menuVos) {
            if (v.getParentMenuId()==1){
                v.setSonMenu(this.findChildNode(v.getMenuId(),menuVos));
                sysMenuVos.add(v);
            }
        }
        return sysMenuVos;
    }

    //递归查找子节点
    private List<SysMenuVo> findChildNode(Integer pid, List<SysMenuVo> menuVos) {
        List<SysMenuVo> sonNodes = new ArrayList<>();
        for (SysMenuVo node:menuVos){
            if (node.getParentMenuId().equals(pid)){
                node.setSonMenu(findChildNode(node.getMenuId(), menuVos));
                sonNodes.add(node);
            }
        }
        return sonNodes;
    }

    /**
     * 通过角色ID查询该角色没有的菜单
     *
     * @param roleId
     * @return
     */
    @Override
    public List<SysMenuEntity> getRoleNotHaveMenus(Integer roleId) {
        List<SysMenuEntity> notHaveMenus = sysMenuMapper.getRoleNotHaveMenus(roleId);
        /*List list =new ArrayList<MenuNotInRoleVo>();
        MenuNotInRoleVo notInRoleVo =null;
        for (SysMenuEntity v: notHaveMenus){
            notInRoleVo=new MenuNotInRoleVo();
            notInRoleVo.setMenuId(v.getMenuId());
            notInRoleVo.setMenuName(v.getMenuName());
            notInRoleVo.setMenuDisc(v.getMenuDiscript());
            notInRoleVo.setMenuStatus(v.getMenuStatus());
            list.add(notInRoleVo);
        }*/
        return notHaveMenus;
    }


}
